//===============================================================================
// Microsoft Aspiring Software Architects Program
// E-Appointments - Case Study Implementation
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using EAppointments.BMS.Services;
using EAppointments.BMS.Services.Contracts;
using System.Diagnostics;
using WFExtensions;

namespace EAppointments.BMS.Hosts.ConsoleHost
{
    class Program
    {
        private static ServiceHost appointmentServiceHost;
        private static ServiceHost providerServiceHost;
        private static ServiceHost directoryServiceHost;

        static void Main(string[] args)
        {
            CreateServiceHosts();

            OnStart();

            // Wait for input from the user
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\n\nPress any key to terminate the service hosts:");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey();

            OnStop();
        }

        /// <summary>
        /// Create the Service Hosts at the Specified Ports
        /// </summary>
        private static void CreateServiceHosts()
        {
            try
            {
                appointmentServiceHost = new WFServiceHost(typeof(EAppointments.BMS.Services.AppointmentService), new Uri("http://localhost:24444/EAppointments.BMS.Host/AppointmentService.svc"));
                OutputServiceInformation(appointmentServiceHost);

                providerServiceHost = new ServiceHost(typeof(EAppointments.BMS.Services.ProviderService), new Uri("http://localhost:24444/EAppointments.BMS.Host/ProviderService.svc"));
                OutputServiceInformation(providerServiceHost);

                directoryServiceHost = new ServiceHost(typeof(EAppointments.BMS.Services.DirectoryService), new Uri("http://localhost:24444/EAppointments.BMS.Host/DirectoryService.svc"));
                OutputServiceInformation(directoryServiceHost);


            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw;
            }

        }

        /// <summary>
        /// Open the Service Hosts
        /// </summary>
        protected static void OnStart()
        {
            try
            {
                appointmentServiceHost.Open();
                providerServiceHost.Open();
                directoryServiceHost.Open();

            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw;
            }

        }

        /// <summary>
        /// Close the Service Hosts
        /// </summary>
        protected static void OnStop()
        {
            appointmentServiceHost.Close();
            providerServiceHost.Close();
            directoryServiceHost.Close(); 

        }

        /// <summary>
        /// This method will write out the information about a service host to the console.  
        /// </summary>
        /// <param name="hostOrderService"></param>
        private static void OutputServiceInformation(ServiceHostBase serviceHost)
        {
            string endpointAddresses = GetEndpointAddresses(serviceHost.Description);
            Console.WriteLine(string.Format("\n\r The {0} service is running at:{1}{2}", serviceHost.Description.Name, Environment.NewLine, endpointAddresses));
        }

        /// <summary>
        /// This method will return a formatted string of endpoints for a given ServiceDescription.
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        private static string GetEndpointAddresses(System.ServiceModel.Description.ServiceDescription description)
        {
            // Create a new string builder for managing the output string
            StringBuilder stringBuilder = new StringBuilder();

            // Enumerate through each of the ServiceEndPoints and append them to the string.
            foreach (System.ServiceModel.Description.ServiceEndpoint serviceEndPoint in description.Endpoints)
            {
                stringBuilder.AppendFormat("\n\t{0}", serviceEndPoint.Address.ToString());
            }

            // Return the formatted EndPoints
            return stringBuilder.ToString();
        }

    }
}

